/**
 * @author {Yuntao Zhou}
 */
var NewsDetailWin  = null;
var NewsScrollPN=null;
var workspaceManager={};
var rtqWindowGroup=null;
workspaceManager.openNewsDetailWin = function(title,content,url){       
	if (!rtqWindowGroup) {
        rtqWindowGroup = new Util.WindowGroup(document.body, {}, {});
    }
	if(!NewsDetailWin){
		NewsDetailWin=new Util.RtqWindow(document.body, {
			windowReady:function(obj){
				obj.url=url;
				obj.setHtml('<div class="rtq-newsdetail"><div class="content"><div class="data"></div></div><div class="action"><a class="close" href="javascript:void(0)">Close</a></div></div>');
				var El = obj.el;
				var close=$('a.close',El);
				var contentEl = El.find('.content');
				var dataEl = $('.content .data', El);
				if (NewsScrollPN == null) {
					NewsScrollPN = new Util.ScrollPanel(dataEl, {
						showX: false
					});
				}
				if (!obj.url) {
					NewsScrollPN.el.show();
					dataEl.html(content);
					dataEl.css({
						height: 410
					});
					NewsScrollPN.reSize(660, 410);
				}else{
					NewsScrollPN.el.hide();
					dataEl.appendTo(contentEl);
					dataEl.html(content+'<br><iframe frameborder="0" src="'+obj.url+'" style="width:660px;height:380px"></iframe>');				
				}				
				close.bind("click.newsDetail",function(e){
					obj.hide();
				});
			},
            beforeClosed:function(){},
			onResized:function(obj,w,h){
				var contentEl = obj.el.find('.content');
				var dataEl = obj.el.find('.content .data');
					contentEl.css({
						height: h - 90
					});
					dataEl.css({
						width:w-40,
						height: h - 90
					});
				if (!obj.url) {	
					NewsScrollPN.reSize(w - 40, h - 90);
				}else{
					dataEl.find('iframe').css({
						width:w-40,
						height:h-120
					});	
				}
			}
		},{
			title:title,
			top:80,
            width: 700,
            height: 500,
            resizable: true,
            isGlobal:true,
            hasSettingPanel: false,
            closeAction:'hide'
		});
		rtqWindowGroup.register(NewsDetailWin);
	}else{	
		NewsDetailWin.show();
        NewsDetailWin.setTitle(title); 
		NewsDetailWin.url=url;
		var el=NewsDetailWin.el,
		w=el.width(),
		h=el.height();
		var contentEl=el.find('.content');
        var dataEl=$('.content .data',el);
		if (!NewsDetailWin.url){
			NewsScrollPN.el.show();
			dataEl.appendTo(NewsScrollPN.el);
			dataEl.html(content);
			dataEl.css({
				height: h-90
			});
			NewsScrollPN.reSize(w-40, h-90);
		}else{
			NewsScrollPN.el.hide();
			dataEl.appendTo(contentEl);
			dataEl.css({
				marginRight:0,
				width:w-40
			});
			dataEl.html(content+'<br><iframe frameborder="0" src="'+NewsDetailWin.url+'"></iframe>');	
			dataEl.find('iframe').css({
				width:w-40,
				height:h-120
			});			
		}
	} 
};

var Cookies = {
     get: function(name) {
         var arg = name + "=";
         var alen = arg.length;
         var clen = document.cookie.length;
         var i = 0;
         var j = 0;
         while (i < clen) {
             j = i + alen;
             if (document.cookie.substring(i, j) == arg)
                 return Cookies.getCookieVal(j);
             i = document.cookie.indexOf(" ", i) + 1;
             if (i == 0)
                 break;
         }
         return null;
     },
     getCookieVal: function(offset) {
         var endstr = document.cookie.indexOf(";", offset);
         if (endstr == -1) {
             endstr = document.cookie.length;
         }
         return unescape(document.cookie.substring(offset, endstr));
     }
 };
//var QS_VERSION='dev';
var QS_VERSION='release';
var QS_PROTOCOL="http://";
if (typeof(versions) == "undefined") {
    versions = {
        dev: {
            rtq:  QS_PROTOCOL + "rtqdev.morningstar.com/opt/",
            push: QS_PROTOCOL + "pushdev.morningstar.com"
        },
        pre: {
            rtq:  QS_PROTOCOL + "quotespeed.morningstar.com/",
            push: QS_PROTOCOL + "push.morningstar.com"
        },
        release: {
            rtq:  QS_PROTOCOL + "quotespeed.morningstar.com/",
            push: QS_PROTOCOL + "push.morningstar.com"
        }
    };
}
if (typeof(QS_VERSION) == "undefined") {
    var version = versions.release;
}
else {
    var version = versions[QS_VERSION];
}

var reloginUrl = version.rtq+"newsdemologin.jsp?r=newsdemo.html";    // when session expired or can't login redirect this page.
if(Cookies.get("UsrID")==null||Cookies.get("UserTID")==null||Cookies.get("SessionID") == null){
	//window.location.href = reloginUrl;
}

var QS_UserID=Cookies.get("UsrID");
var QS_UserTID=Cookies.get("UserTID");

DBHelper = {};
DBHelper.URLs = {};

DBHelper.URLs.template = version.rtq + "template.jsp";
//Template
DBHelper.getWidgetTemplate = function(TemplateId, callback){
    //if (QS_UserID != null) {
        var data = { tplId: TemplateId.toString().replace(/[^0-9]/g, ''), action:'gettmp', tmpid:QS_UserTID };
        $.post(DBHelper.URLs.template, data, callback);
    //}
};
DBHelper.getWidgetTemplateListByType = function(TemplateType, callback){
    //if (QS_UserID != null) {
        var data = { type: TemplateType, action:'gettmplist', tmpid:QS_UserTID };
        $.post(DBHelper.URLs.template, data, callback);
    //}
};


if (!GOLBAL_DATA) {
    var GOLBAL_DATA = {};
}
//widget Template
GOLBAL_DATA.widgetTemplateList = {};

GOLBAL_DATA.widgetTemplateList['2'] = []; //News
GOLBAL_DATA.widgetTemplateList['21']=[];//News Custom view

GOLBAL_DATA.refreshTemplateListData = function(type,callback){
    DBHelper.getWidgetTemplateListByType(type, function(d){
        if(d == "")return;
        d = eval("("+d+")");
        GOLBAL_DATA.widgetTemplateList[type] = d.temp;
        if(callback){
            callback(GOLBAL_DATA.widgetTemplateList[type]);
        }
    });
};
GOLBAL_DATA.getTemplateListData = function(type, callback){
    if (GOLBAL_DATA.widgetTemplateList[type] && GOLBAL_DATA.widgetTemplateList[type].length > 0) {
        callback(GOLBAL_DATA.widgetTemplateList[type]);
    }
    else {
        GOLBAL_DATA.refreshTemplateListData(type, callback);
    }
};